var app = new Vue({
    el: ".transfer_wrap",
    data(){
        return {
            showeamil: '', 
            Prohibited: false, //禁用按钮多次点击,
            transemail:'', //转账邮箱
            transnum:'', //转账金额
            Requestuser:'', //转账对象用户名
            TransferuserID: '',//转账对象id
            theuser: '',
            information: '',
            HPS: 'HPS',
            ETH: 'ETH',
            BTC: 'BTC',
            HSR: 'HSR'
        }
    },
    computed: {
        fee1: {
            get() {
                let amount = this.transnum || ''
                let patt1 = /[a-z]+ /.test(amount);
                if (amount == '' || amount == null || amount < 0 || !patt1) amount = 0;
                amount = this.transnum - (this.transnum * 3 / 1000)
                if (isNaN(amount)) {
                    amount = 0
                    return amount
                }
                return amount
            }
        }
    },
    created(){
        var test = window.location.href;
        let id = test.substring(test.indexOf("id") + 3, test.indexOf("&"));
        let coin = test.substring(test.indexOf("name") + 5);
        this.showeamil = coin;
    },
    methods: {
        getCookie(cookiename) { //获取cookie
            var arr,
                reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if ((arr = document.cookie.match(reg))) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        },
        closeinfo() { //关闭弹窗
            let timer=setInterval(() => {
                document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                clearInterval(timer)
                timer=null
            },2000)
            document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
            this.theuser = "";
            this.information = ""
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = ""
        },
        Requestuserinformation() {//邮箱请求用户信息
            let _this = this;
            let Email = this.transemail;
            if (Email != "") {
                let params = {
                    email: Email
                };
                axios.post("/user/public", params).then(res => {
                    if (res.data.code == 200) {
                        if (res.data.data) {
                            _this.TransferuserID = res.data.data.id;
                            _this.Requestuser = res.data.data.nickname;
                        }
                    }
                })
            }
        },
        TransferApply(index,coin) {//转账
            let lang = this.getCookie("lang");
            let regValid = (/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
            let that = this;
            let transemail = this.transemail
            let transnum = this.transnum
            let ID = this.TransferuserID
            let blancevalue = document.getElementsByClassName('inpit' + index)[0].value //币种金额
            let UID = window.localStorage.getItem('ID')//用户ID
            if(ID == UID){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser="Transfer Failed"
                    this.information="Can't transfer to yourself."
                } else if (lang == "zh_TW") {
                    this.theuser="轉賬失敗"
                    this.information="不能給自己轉賬。"
                } else {
                    this.theuser="转账失败"
                    this.information="不能给自己转账。"
                }
                this.Requestuser = "";
                return;
            }
            if (transemail == "") {
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser="Transfer Failed"
                    this.information="The mailbox you transferred cannot be empty."
                } else if (lang == "zh_TW") {
                    this.theuser="轉賬失敗"
                    this.information="您轉賬的郵箱不能為空。"
                } else {
                    this.theuser="转账失败"
                    this.information="您转账的邮箱不能为空。"
                }
                this.Requestuser = "";
                return;
            }else if (!regValid.test(transemail)) {
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser="Transfer Failed"
                    this.information="Please enter the correct email address."
                } else if (lang == "zh_TW") {
                    this.theuser="轉賬失敗"
                    this.information="請您輸入正確的郵箱地址"
                } else {
                    this.theuser="转账失败"
                    this.information="请您输入正确的邮箱地址"
                }
                this.transemail = "";
                this.Requestuser = "";
                return
            }
            if (coin === this.HPS) {
                if (transnum < 1) {
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser="Transfer Failed"
                        this.information="The minimum HPS transfer is 1."
                    } else if (lang == "zh_TW") {
                        this.theuser="轉賬失敗"
                        this.information="HPS最小轉賬為1。"
                    } else {
                        this.theuser="转账失败"
                        this.information="HPS最小转账为1。"
                    }
                    this.transnum = 0;
                    return;
                } else {
                    if (blancevalue - transnum < 0) {
                        let timer=setInterval(() => {
                            document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                            clearInterval(timer)
                            timer=null
                        },2000)
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                        if (lang == "en_US") {
                            this.theuser="Transfer Failed"
                            this.information="Your balance is insufficient and the transfer failed."
                        } else if (lang == "zh_TW") {
                            this.theuser="轉賬失敗"
                            this.information="您的餘額不足，轉賬失敗。"
                        } else {
                            this.theuser="转账失败"
                            this.information="您的余额不足，转账失败。"
                        }
                        this.transnum = 0;
                        return;
                    }
                }
            } else if (coin == this.ETH) {
                if (transnum < 0.1) {
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser="Transfer Failed"
                        this.information="The minimum ETH transfer is 0.1."
                    } else if (lang == "zh_TW") {
                        this.theuser="轉賬失敗"
                        this.information="ETH最小轉賬為0.1。"
                    } else {
                        this.theuser="转账失败"
                        this.information="ETH最小转账为0.1。"
                    }
                    this.transnum = 0;
                    return;
                } else {
                    if (blancevalue - transnum < 0) {
                        let timer=setInterval(() => {
                            document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                            clearInterval(timer)
                            timer=null
                        },2000)
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                        if (lang == "en_US") {
                            this.theuser="Transfer Failed"
                            this.information="Your balance is insufficient and the transfer failed."
                        } else if (lang == "zh_TW") {
                            this.theuser="轉賬失敗"
                            this.information="您的餘額不足，轉賬失敗。"
                        } else {
                            this.theuser="转账失败"
                            this.information="您的余额不足，转账失败。"
                        }
                        this.transnum = 0;
                        return;
                    }
                }
            } else if (coin == this.BTC) {
                if (transnum < 0.001) {
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser="Transfer Failed"
                        this.information="The minimum BTC transfer is 0.001."
                    } else if (lang == "zh_TW") {
                        this.theuser="轉賬失敗"
                        this.information="BTC最小轉賬為0.001。"
                    } else {
                        this.theuser="转账失败"
                        this.information="BTC最小转账为0.001。"
                    }
                    this.transnum = 0;
                    return;
                } else {
                    if (blancevalue - transnum < 0) {
                        let timer=setInterval(() => {
                            document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                            clearInterval(timer)
                            timer=null
                        },2000)
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                        if (lang == "en_US") {
                            this.theuser="Transfer Failed"
                            this.information="Your balance is insufficient and the transfer failed."
                        } else if (lang == "zh_TW") {
                            this.theuser="轉賬失敗"
                            this.information="您的餘額不足，轉賬失敗。"
                        } else {
                            this.theuser="转账失败"
                            this.information="您的余额不足，转账失败。"
                        }
                        this.transnum = 0;
                        return;
                    }
                }
            }
            let params = {
                coin: coin,     //币种
                userId: UID,    //用户id
                a: transnum,    //转账金额
                u: ID,          //对方id
                e: transemail,  //对方邮箱
            };
            axios.post("/adress/coin/transfer", params).then(res => {
                if (res.data.code == 200) {
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser="Successful"
                        this.information="Your currency has been successfully transferred."
                    } else if (lang == "zh_TW") {
                        this.theuser="轉賬成功"
                        this.information="您的幣已經成功轉出。"
                    } else {
                        this.theuser="转账成功"
                        this.information="您的币已经成功转出。"
                    }
                    this.transemail = "";
                    this.Requestuser = "";
                    this.transnum = 0;
                    return;
                }else{
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser="Transfer Failed"
                        this.information="Your transfer email does not exist."
                    } else if (lang == "zh_TW") {
                        this.theuser="轉賬失敗"
                        this.information="您的轉賬郵箱不存在。"
                    } else {
                        this.theuser="转账失败"
                        this.information="您的转账邮箱不存在。"
                    }
                    this.transemail = "";
                    this.Requestuser = "";
                    this.transnum = 0;
                    return;
                }
            });
        }
    }
})